# -*- coding: utf-8 -*-

class logger(object):

    @staticmethod
    def _is_tty():
        _isatty = getattr(sys.stdout, 'isatty', None)
        return _isatty and _isatty()

    @staticmethod
    def _check_stdout_stderr_same():
        stdout_fileno = getattr(sys.stdout, "fileno", lambda: 1)()
        stderr_fileno = getattr(sys.stderr, "fileno", lambda: 2)()
        return stdout_fileno == stderr_fileno

    @staticmethod
    def _format_msg(msg, args):
        return (msg % args) if args else msg

    @ classmethod
    def _log(cls, asctime, level, msg, args, file=sys.stdout):
        fiedls = [str(asctime), level.upper(), cls._format_msg(msg, args)]
        print(" - ".join(fiedls), file=file)

    @classmethod
    def info(cls, msg, *args):
        cls._log(now(), "info", msg, args)

    @classmethod
    def warn(cls, msg, *args):
        cls._log(now(), "warn", msg, args)

    warning = warn

    @classmethod
    def error(cls, msg, *args):
        asctime = now()
        cls._log(asctime, "error", msg, args, file=sys.stderr)
        if cls._is_tty() or cls._check_stdout_stderr_same():
            return
        cls._log(asctime, "error", msg, args, file=sys.stdout)
